package com.example.demo.Mappers;


import com.example.demo.DynaSQL.NaExamProvider;
import com.example.demo.beans.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by Administrator on 2018/4/4.
 */
@Mapper
public interface RegistMapper {
    @Insert("INSERT INTO AutoRegist(CardNo,CardType,PatientID,Name," +
            "Sex,Birthday,IDCardNo,Mobile,Address,PayTag, PayAmt,BankCardNo," +
            "BankTranNo,\n" +
            "BankReferNo, BankPNo,\n" +
            "BankTermid,CodeBuyerAccount, CodeTradeOrderNo,CodeTermidNo," +
            "PayTranDate,PayTranTime,AutoTranNo,UserId,reqtype)\n" +
            "VALUES(#{CardNo},#{CardType},#{PatientID},#{Name},#{Sex}," +
            "#{Birthday},#{IDCardNo},#{Mobile},#{Address},#{PayTag},#{PayAmt}," +
            "#{BankCardNo},#{BankTranNo},#{BankReferNo},#{BankPNo},#{BankTermid}" +
            ",#{CodeBuyerAccount},#{CodeTradeOrderNo},#{CodeTermidNo}" +
            ",#{PayTranDate},#{PayTranTime},#{AutoTranNo},#{UserId},#{reqtype})")
    /**保存到注册记录表**/
    void saveregist(RegistPatientInfoRequest registPatientInfoRequest);

   @Insert("INSERT INTO ghb_jsjl ( jsjlid, ghxh, jscs, czybm, ywckbm, brid, fyhj," +
 " ylkzf, xjzf, ybkzf, tbje, jsqf, qfbj, jszt, jsrq, czyks, bzms, bxjsbh,ybje, mbbzje, ylkyhje, " +
     "ylkh, ybkzhye, yhje, zeyh, qtzf )VALUES " +
"( #{jsjlid}, #{ghxh},#{jscs},#{czybm},#{ywckbm},#{brid},#{fyhj},#{ylkzf},#{xjzf},#{ybkzf},#{tbje}," +
"#{jsqf},#{qfbj}, #{jszt},#{jsrq},#{czyks},#{bzms}, #{bxjsbh},#{ybje},#{mbbzje},#{ylkyhje},#{ylkh}," +
 " #{ybkzhye},#{yhje},#{zeyh}, #{qtzf})")
   /**保存到结算记录**/
   void saveghjsb(JsjlBean jsjlBean);

    @Insert("insert ghb_zcxx(brid,czybm,ywckbm,brxm,brxb,brsr,brnl,brnldw,sfzh,jtzz,sj,zcrq,lxrdh)" +
            "values(#{brid},#{czybm},#{ywckbm},#{brxm},#{brxb},#{brsr},#{brnl},'1',#{sfzh},#{jtzz},#{sj},#{zcrq},#{sj})")
/**
 * 插入病人注册表
 */
    void savebrzcb(@Param("brid") String brid, @Param("czybm") String czybm, @Param("ywckbm") String ywckbm, @Param("brxm") String brxm, @Param("brxb") String brxb,
                   @Param("brsr") String brsr, @Param("brnl") String brnl, @Param("sfzh") String sfzh, @Param("jtzz") String jtzz, @Param("sj") String sj, @Param("zcrq") String zcrq);

    @Insert("insert into ghb_mzylkxx(ylkh,brid,czybm,ylklxbm,czyks,dkrq)" +
            "values(#{ylkh},#{brid},#{czybm},#{ylklxbm},#{czyks},#{dkrq})")
/**绑定医疗卡号**/
    void savergistylk(@Param("ylkh") String ylkh, @Param("brid") String brid, @Param("czybm") String czybm, @Param("ylklxbm") String ylklxbm, @Param("czyks") String czyks, @Param("dkrq") String dkrq);

    @Insert("insert into ghb_brgh(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,ghks,brnl,brnldw,ghrq,jzrq,ywckbm,jzys)values" +
            "(#{ghxh},#{ghzlbm},#{ghfsbm},#{fbbm},#{brid},#{czybm},#{czyks},#{ghks},#{brnl},'1',#{ghrq},#{jzrq},#{ywckbm},#{jzys})")
/**插入病人挂号表**/
    void saveghb(@Param("ghxh") String ghxh, @Param("ghzlbm") String ghzlbm, @Param("ghfsbm") String ghfsbm
            , @Param("fbbm") String fbbm, @Param("brid") String brid, @Param("czybm") String czybm, @Param("czyks") String czyks,
                 @Param("ghks") String ghks, @Param("brnl") String brnl, @Param("ghrq") String ghrq, @Param("jzrq") String jzrq, @Param("ywckbm") String ywckbm, @Param("jzys") String jzys);

    @Insert(" insert into mzb_brfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,sfrq,rybrid," +
            "ryghxh,brxm,fysl,fydj,fyje,mzks,mzys,fphm,ryjsjlid,sfjs,jscs)" +
 "values(#{fbbm},#{ywckbm},#{xlbm},#{mxfyxmbm},#{czybm},#{dlbm},#{sfrq}," +
"#{rybrid},#{ryghxh},#{brxm},#{fysl},#{fydj},#{fyje},#{mzks},#{mzys}," +
            "#{fphm},#{ryjsjlid},#{sfjs},#{jscs})")
/**保存病人费用信息**/
void savemzbbrfy(@Param("fbbm") String fbbm, @Param("ywckbm") String ywckbm,
                 @Param("xlbm") String xlbm,
@Param("mxfyxmbm") String mxfyxmbm, @Param("czybm") String czybm,
@Param("dlbm") String dlbm, @Param("sfrq") String sfrq, @Param("rybrid") String rybrid,
@Param("ryghxh") String ryghxh, @Param("brxm") String brxm, @Param("fysl") String fysl,
  @Param("fydj") String fydj, @Param("fyje") String fyje,
  @Param("mzks") String mzks, @Param("mzys") String mzys,@Param("fphm")String fphm,
 @Param("ryjsjlid") String ryjsjlid,@Param("sfjs")String sfjs,@Param("jscs")String jscs );

@Insert("INSERT INTO ghb_yygh ( yyghid, ywckbm, czybm,brid, yyghrq, brxm, brxb, brsr,brnl, brnldw, sfzh," +
            " jtzz, sj,yyys, yyks,yydjrq, yyyxrq, czyks,ylkh,yyjfbz)values" +
            "(#{yyghid},#{ywckbm},#{czybm},#{brid},#{yyghrq},#{brxm},#{brxb},#{brsr},#{brnl},#{brnldw},#{sfzh}," +
            "#{jtzz},#{sj},#{yyys},#{yyks},#{yydjrq},#{yyyxrq},#{czyks},#{ylkh},#{yyjfbz})")
void saveyyghb(@Param("yyghid")String yyghid ,@Param("ywckbm")String ywckbm ,@Param("czybm")String czybm
,@Param("brid")String brid ,@Param("yyghrq")String  yyghrq,@Param("brxm")String brxm ,@Param("brxb")String brxb ,@Param("brsr")String brsr
,@Param("brnl")String brnl ,@Param("brnldw")String brnldw  ,@Param("sfzh")String sfzh  ,@Param("jtzz")String jtzz  ,@Param("sj")String sj
        ,@Param("yyys")String yyys ,@Param("yyks")String yyks
        ,@Param("yydjrq")String yydjrq  ,@Param("yyyxrq")String yyyxrq  ,@Param("czyks")String czyks  ,@Param("ylkh")String ylkh  ,@Param("yyjfbz")int yyjfbz  );



    @Select(" BEGIN TRANSACTION select top 1  xhlx,xh,ssrq,cslx  from ghb_ywxhb WITH (TABLOCKX) where xhlx='brid'")
//    @Results({
//            @Result(column = "xhlx", property = "xhlx"),
//            @Result(column = "xh", property = "xh"),
//            @Result(column = "ssrq", property = "ssrq"),
//            @Result(column = "cslx", property = "cslx")
//    })
    /**获取业务序号表的注册信息**/
    Brzc_YwxhBean getBrzcYwxh();

    @Select(" BEGIN TRANSACTION select top 1  xhlx,xh,ssrq,cslx  from ghb_ywxhb WITH (TABLOCKX) where xhlx='ghxh'")
//    @Results({
//            @Result(column = "xhlx", property = "xhlx"),
//            @Result(column = "xh", property = "xh"),
//            @Result(column = "ssrq", property = "ssrq"),
//            @Result(column = "cslx", property = "cslx")
//    })
/**获取挂号序号中的业务序号表**/
    Ghxh_YwckBean getGhxhYwxh();

  @Select(" BEGIN TRANSACTION select top 1  xhlx,xh,ssrq,cslx  from mzys_ywxhb WITH (TABLOCKX) where xhlx='jysq'")
  //获取检验申请的业务序号
  Jysqh_YwckBean getJysqxhYwxh();

    @Select(" BEGIN TRANSACTION  select top 1  xhlx,xh,ssrq,cslx  from ghb_ywxhb  WITH (TABLOCKX) where xhlx='yyghid'")
    Yygh_ywxh getyyghywxh();
    @Select(" BEGIN TRANSACTION select top 1  xhlx,xh,ssrq,cslx  from ghb_ywxhb WITH (TABLOCKX) where xhlx='jsjlid'")
//    @Results({
//            @Result(column = "xhlx", property = "xhlx"),
//            @Result(column = "xh", property = "xh"),
//            @Result(column = "ssrq", property = "ssrq"),
//            @Result(column = "cslx", property = "cslx")
//    })
    /**获取业务序号表中结算记录的信息**/
Jsjl_YwxhBean getJsjlywxh();
    @Select("SELECT '0' ResultCode,'交易成功，该卡已经绑定！'ResultMsg " +
            ",RTRIM(brid)PatientID, zcrq as OpDateTime ,''HosTranNo FROM v_ghb_zcxx where brid=#{brid}")
    @Results({
            @Result(column = "ResultCode", property = "resultCode"),
            @Result(column = "ResultMsg", property = "resultMsg"),
            @Result(column = "PatientID", property = "patientID"),
            @Result(column = "OpDateTime", property = "opDateTime"),
            @Result(column = "HosTranNo", property = "hosTranNo")
    })
    RegistPatientInfoResponse getRegistPoseByid(String brid);
@Select("SELECT qsh , jsh , dqfplx , lpfs , fpjlid FROM gyb_mzdqpj WHERE dqczy =#{dqczy} and dqfplx ='2' and lpfs ='0' and fplx ='0'")
   /**获取门诊票据信息**/
    MzdqpjBean Getmzpjxx(@Param("dqczy") String dqczy );

   @Select("select  top 1 rtrim(brid)PatientID,dkrq as 'OpDateTime' from ghb_mzylkxx where ylkh=#{ylkh} ")
   RegistOldBean getpainfo(@Param("ylkh")String ylkh);

    @Select("declare @book_code varchar(50)set @book_code=(select max(right('000000',6)) where '000000' like '%')+#{id} set @book_code=''+right('000000'+@book_code,6) select @book_code  as xh")
    /**生成业务序号表中的序号**/
    YwxhBean selectywxhbyid(@Param("id") int id);
@Update("update ghb_ywxhb set xh=#{xh},ssrq=#{ssrq} where xhlx='brid' commit TRANSACTION ")
    /**更细业务序号表中病人注册ID的相关信息**/
    void updatebrzcywxh(@Param("xh") String xh, @Param("ssrq") String ssrq);

    @Update("update ghb_ywxhb set xh=#{xh},ssrq=#{ssrq} where xhlx='ghxh' commit TRANSACTION ")
/**更新业务序号表总挂号序号的相关信息**/
    void updateghxhywxh(@Param("xh") String xh, @Param("ssrq") String ssrq);
    @Update(" update mzys_ywxhb set xh=#{xh},ssrq=#{ssrq} where xhlx='jysq' commit TRANSACTION ")
    //更新检验申请单业务序号
    void updatejysqdywxh(@Param("xh") String xh, @Param("ssrq") String ssrq);

    @Update("update ghb_ywxhb set xh=#{xh},ssrq=#{ssrq} where xhlx='jsjlid' commit TRANSACTION ")
    /**更新业务序号表中结算记录ID相关信息**/
    void updejsjlywxh(@Param("xh") String xh,@Param("ssrq") String ssrq);
    @Update("update ghb_ywxhb set xh=#{xh},ssrq=#{ssrq} where xhlx='yyghid' commit TRANSACTION")
    void updateyyghxh(@Param("xh")String xh,@Param("ssrq")String ssrq);

@Update("UPDATE gyb_mzdqpj SET qsh = #{qshnew} WHERE qsh = #{qshold} AND jsh = #{jsh} AND fpjlid = #{fpjlid} AND dqfplx = '2' AND lpfs = '0' AND dqczy = #{dpczy} ")
/**
 * 更新发票信息
 */
void updatemzdqqj(@Param("qshnew")String qshnew,@Param("qshold")String qshold,@Param("jsh")String jsh,@Param("fpjlid")String fpjlid,@Param("dpczy")String dpczy);


    @Select("select top 1 RTRIM(CardNo)CardNo,RTRIM(PatientID)PatientID,RTRIM(Name)Name, RTRIM(Sex)Sex,Age,BANLACE,RTRIM(IDCardNo)IDCardNo,Birthday,CardStatus,ylklxbm,ylkzt,RTRIM(Mobile)Mobile,RTRIM(Address)Address" +
            " from AutoView_PatientInfo where PatientID=#{PatientID} and CardNo=#{CardNo} and ylklxbm<>'02' ")
    //获取病人详细信息
    PatientInfodetail selectpatientdetal(@Param("PatientID") String PatientID,@Param("CardNo")String CardNo);
@Select("select top 1 DoctID,DoctName,TypeName,DepName,DeptID,RankName,Specialty,TreatLocation,TimeRegion,(yghs+yyys)WaitNum,xhzs,xyzs,yghs,yyys,TypeID ,bcfabm from AutoView_RegDoctList" +
        " where DoctID=#{DoctID} AND DAY=#{DAY}")
    DocDetailBean selectdocdetail(@Param("DoctID")String DoctID,@Param("DAY")String DAY);

@Update("UPDATE ghb_yzhb SET yyys=yyys+#{i},yghs=yghs+#{k} where Yzrq=#{Yzrq}  and czybm=#{czybm}")
    void  updateyzhb(@Param("i")int i,@Param("k")int k,@Param("Yzrq")String Yzrq,@Param("czybm")String czybm);
@Select("select a.czybm,a.czyxm,a.ghzlbm,b.mxfyxmbm,c.mxfyxmmc,c.xlbm,c.fydj, d.dlbm from gyb_czy a (nolock) left join ghb_hbsfxm b (nolock) on a.ghzlbm=b.ghzlbm left join gyb_mxfyxm c (nolock) on b.mxfyxmbm=c.mxfyxmbm left join gyb_fyxl d (nolock) on c.xlbm=d.xlbm " +
        "where czybm=#{czybm} and c.mxfyxmbm<>'080137'   order by a.czybm")
    List<GhzlbmBean>selectghzl(@Param("czybm")String czybm);

@Update("update mzb_brfy set ryjsjlid=#{jsjlid},jscs='1',sfjs='1' where ryghxh=#{ghxh} and jscs='1' and sfjs='0'")
   /**更新门诊表病人费用表**/
void updatebrfy(@Param("jsjlid")String jsjlid,@Param("ghxh")String ghxh);
@Update("update ghb_yygh set yyclbz='1' where yyghid=#{yyghid}")
  /**更新预约处理标志**/
void updateyyclbz(@Param("yyghid")String yyghid);
//
//    @Insert("INSERT INTO AutoRegist(CardNo,CardType,PatientID,Name," +
//            "Sex,Birthday,IDCardNo,Mobile,Address,PayTag, PayAmt,BankCardNo," +
//            "BankTranNo,\n" +
//            "BankReferNo, BankPNo,\n" +
//            "BankTermid,CodeBuyerAccount, CodeTradeOrderNo,CodeTermidNo," +
//            "PayTranDate,PayTranTime,AutoTranNo,UserId,bzms,jsxh,reqtype,czybm,czyxm)\n" +
//            "VALUES(#{CardNo},#{CardType},#{PatientID},#{Name},#{Sex}," +
//            "#{Birthday},#{IDCardNo},#{Mobile},#{Address},#{PayTag},#{PayAmt}," +
//            "#{BankCardNo},#{BankTranNo},#{BankReferNo},#{BankPNo},#{BankTermid}" +
//            ",#{CodeBuyerAccount},#{CodeTradeOrderNo},#{CodeTermidNo}" +
//            ",#{PayTranDate},#{PayTranTime},#{AutoTranNo},#{UserId},#{bzms},#{jsxh},#{reqtype}),#{czybm},#{czyxm}")
//    /**保存到支付记录表中**/
//    void saverePaybean(PayDetailBean payDetailBean);
    @InsertProvider(type = NaExamProvider.class,method = "InsertAutoRegist")
    /**保存到支付记录表中**/
    void saverePaybean(PayDetailBean payDetailBean);
//    @Insert("insert into AutoOrderList(OrderDate,TypeID,TypeName,DeptName,DeptID,DoctID,DoctName," +
//  "RankName,RegID,AppFee,RegFee,TreatFee,OtherFee,TimeRegion,Specialty,IsAvailable,RegWay,IsPay," +
//  "PatientID,CardNo,yyghid,ghxh,czybm,czyxm)values(#{OrderDate},#{TypeID},#{TypeName},#{DeptName},#{DeptID},#{DoctID},#{DoctName}," +
//            "#{RankName},#{RegID},#{AppFee},#{RegFee},#{TreatFee},#{OtherFee},#{TimeRegion},#{Specialty}," +
//            "#{IsAvailable},#{RegWay},#{IsPay},#{PatientID},#{CardNo},#{yyghid},#{ghxh}),#{czybm},#{czyxm}")
//    /**保存预约记录**/
//
//    void saveyyjl(AutoOrderBean orderBean);

    @InsertProvider(type = NaExamProvider.class,method = "InsertIntoGhb")
    /***保存到挂号表**/
    void saveghbbrgh(BrghBean b);
    @InsertProvider(type = NaExamProvider.class,method = "InsertAutoOrderList")
    /**保存预约记录**/
    void saveyyjl(AutoOrderBean orderBean);
    @Select("select OrderDate,TypeID,TypeName,DeptID,DoctID,RankName,RegID,AppFee,RegFee,TreatFee,OtherFee,TimeRegion,Specialty,IsAvailable,RegWay,IsPay,PatientID,CardNo,yyghid,ghxh from AutoOrderList with(nolock)  where IsPay='1'AND ghxh='' and OrderDate>=(select CONVERT(varchar(10),GETDATE(),120)+' 00:00:00.000') and OrderDate<=(select CONVERT(varchar(10),GETDATE(),120)+' 00:30:00.000') and czybm=#{czybm} and zfbz not in('1')")
    /**获取预约挂号记录**/
    List<OrderBean> getAutoBean(@Param("czybm")String czybm);
    @Select("select a.id, OrderDate,TypeID,TypeName,DeptID,DoctID,RankName,RegID,AppFee,RegFee,TreatFee,OtherFee,TimeRegion,Specialty,IsAvailable,RegWay,IsPay,a.PatientID,a.CardNo,yyghid,a.ghxh,b.ID as autonetid from AutoOrderList a with(nolock) inner join AutoRegist b on a.id=b.AutoOrderId  where IsPay='1'AND ghxh='' and OrderDate>=(select CONVERT(varchar(10),GETDATE(),120)+' 00:00:00.000') and OrderDate<=(select CONVERT(varchar(10),GETDATE(),120)+' 23:30:00.000') and a.zfbz not in('1') and a.czybm=#{czybm}")
    /**获取预约挂号记录**/
    List<reserOrder> getAutoOrderBean(@Param("czybm")String czybm);
    @Update("Update AutoOrderList set ghxh=#{ghxh} where yyghid=#{yyghid} and ghxh='' and PatientID=#{PatientID}")
    void UpdateAutoOder(@Param("ghxh")String ghxh,@Param("yyghid")String yyghid,@Param("PatientID")String PatientID);
 @Update(" update ghb_brgh set sfysghf='1'where ghxh=#{ghxh}  ")
    void updatesfysghf(@Param("ghxh")String ghxh);

@Select("select rtrim(ghzlbm)ghzlbm,SUM(fydj)ghzf from ghb_hbsfxm a with (nolock) left join gyb_mxfyxm b with (nolock)  on a.mxfyxmbm=b.mxfyxmbm where a.mxfyxmbm<>'080137' group by ghzlbm")
  List<GhzlBean> Getghzfy();


    @Select("select top 1 rtrim(AutoTranNo)AutoTranNo ,rtrim(b.ghxh)ghxh,rtrim(b.jsjlid)jsjlid from AutoRegist  a with(nolock) left join ghb_jsjl b with(nolock) on a.ID=b.autonetid where AutoTranNo=#{AutoTranNo}")
        //获取订单号用来判断是否进入系统，如果已经在系统里面则不写入还有挂号序号和结算序号
    ExecRegTempGetIdBean  GetOderIdand(@Param("AutoTranNo")String AutoTranNo);

    @Select("select isnull(SUM(b.fydj),0)ghf from ghb_hbsfxm a with(nolock) left join gyb_mxfyxm b with(nolock) on a.mxfyxmbm=b.mxfyxmbm and a.sffw='0' where a.ghzlbm=#{ghzlbm}")
        //获取应支付挂号费用
    String Getghzf(@Param("ghzlbm") String ghzlbm);


    @Select("select Max(id) as AutoOderListId from AutoOrderList with(nolock)")
        //获取预约记录中的最大id
    String GetMaxAutoOrderListId();

    @Select("select MAX(id) as AutoRegId from AutoRegist")
    /***获取线上交易当时的ID用于更新到结算记录里面**/
    String GetMaxAutoId();

    @Update(" update ghb_brgh set orderId=#{orderId} where ghxh=#{ghxh}")
    /**更新挂号表中的挂号表**/
    void upghbOrderId(@Param("orderId")String orderId,@Param("ghxh")String ghxh);
}
